<?php
/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Model
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Model', 'Model');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */
class AppModel extends Model {
    var $actsAs = array('Containable');
    var $inserted_ids = array();

    public function beforeSave($options = array()) {
        return true;
    }

    function afterSave($created) {
        if($created) {
            $this->inserted_ids[] = $this->getInsertID();
        }
        return true;
    }

    function getInsertedIds($model=NULL){
        //This will not work well for multiple table saving, please rethink logic
        return $this->inserted_ids;
    }

    function getNextAutoIncrement(){
        $table = Inflector::tableize($this->name);
        $info = $this->query("SHOW TABLE STATUS LIKE '$table'");
        return $info[0]['TABLES']['Auto_increment'];
    }

    function formatNumber($value = 0, $type = 'number', $decimal_place = 2){
        if ($type == 'money') {
            $num = number_format($value, $decimal_place, '.', ',');
        }
        else {
            $num = number_format($value, $decimal_place, '.', '');
        }

        return $num;
    }


    function cleanInput($input) {
        $search = array(
            '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
            '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
            '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
            '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
        );
        $output = preg_replace($search, '', $input);
        return $output;
    }

    function sanitize($input) {
        //debug($input);
        if (is_array($input)) {
            $output = array();
            foreach($input as $var=>$val) {
                $output[$var] = $this->sanitize($val);
            }
        }
        else {
            if (get_magic_quotes_gpc()) {
                $input = stripslashes($input);
            }
            $input  = $this->cleanInput($input);
            $new_enough_php = function_exists("mysql_real_escape_string");//i.e. PHP >= v4.3.0
            if($new_enough_php)
            {//PHP v4.3.0 or higher
                //undo any magic quote effects so mysql_real_escape_string can do the work
                // $output = mysql_real_escape_string($input);
            }
            $output = $input;
        }
        return $output;
    }

}